package com.zxcl.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxcl.constants.CategoryConstant;
import com.zxcl.domain.Category;
import com.zxcl.dto.CategoryUpdateDTO;
import com.zxcl.ex.BusinessException;
import com.zxcl.mapper.CategoryMapper;
import com.zxcl.service.impl.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "com.zxcl.service.CategoryServiceImpl")  // 命名隔离  key的前缀
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询所有分类信息
     *
     * @return
     */
    @Override
    @Cacheable(key = CategoryConstant.CATEGORY_LIST)  //可以做缓存，因为里面没有任何条件
    public List<Category> loadCategories() {

        return this.getBaseMapper().selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getStatus, 1)
                .orderByDesc(Category::getSeq, Category::getUpdateTime) //先按照排序码，一样在按照更新时间
        );
    }


    /**
     * 查询可用所有父分类列表
     *
     * @return
     */
    @Override
    @Cacheable(key = CategoryConstant.CATEGORY_PARENT_LIST)
    public List<Category> loadParentCategories() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, 0L)
                .eq(Category::getStatus, 1)
                .orderByDesc(Category::getSeq, Category::getUpdateTime) //降序
        );
    }

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = CategoryConstant.CATEGORY_PARENT_LIST),
            @CacheEvict(key = CategoryConstant.CATEGORY_LIST)
    })  //把缓存清除，因为数据库中的东西变了，缓存数据不变
    public Integer addCategory(Category category) {
        //分类如果存在就不往数据库中加 ，就是判断分类名是否相同
        Long count = categoryMapper.selectCount(new LambdaQueryWrapper<Category>()
                .eq(Category::getCategoryName, category.getCategoryName())
        );
        if (count > 0) {
            throw new BusinessException("该分类已经存在");
        }
        //如果parentId为空或者0，parentid设置为0
        Long parentId = category.getParentId();
        if (ObjectUtils.isEmpty(parentId) || parentId.equals(0L)) {
            category.setParentId(0L);
        } else {
            // 校验
            Category root = categoryMapper.selectById(parentId);
            if (ObjectUtils.isEmpty(root)) {
                throw new BusinessException("该父分类不存在");
            }
        }
        //最后更新为填充的信息
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        return categoryMapper.insert(category);
    }

    /**
     * 修改分类
     *
     * @param categoryUpdateDTO
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = CategoryConstant.CATEGORY_PARENT_LIST),
            @CacheEvict(key = CategoryConstant.CATEGORY_LIST)
    })  //把缓存清除，因为数据库中的东西变了，缓存数据不变
    public Integer updateCategory(CategoryUpdateDTO categoryUpdateDTO) {
        Long categoryId = categoryUpdateDTO.getCategoryId();

        Category category = categoryMapper.selectById(categoryId);
        category.setPic(categoryUpdateDTO.getPic());
        category.setCategoryName(categoryUpdateDTO.getCategoryName());
        category.setParentId(categoryUpdateDTO.getParentId());
        category.setSeq(categoryUpdateDTO.getSeq());
        category.setStatus(categoryUpdateDTO.getStatus());

        int i = categoryMapper.updateById(category);
        return i;
    }


}
